//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using System.ComponentModel;

namespace OpenLS.Chart
{
    ///<summary>
    /// Represents the title of a chart area or an axis
    ///</summary>
    public class Title : IOfficeXmlSerializable, IShape, INotifyPropertyChanged
    {
        public Title()
        {
            
        }

        public Title(TextBody textBody)
        {
            tx = new Text {TextProperties = textBody};
        }
        private ExtensionElement _extLst;
        private Layout layout;
        private bool overlay;
        private ShapeProperties spPr;
        private Text tx;
        private TextBody txPr;

        public Text Text
        {
            get { return tx; }
            set { tx = value;
                notifyPropertyChanged("Text");}
        }

        public Layout Layout
        {
            get { return layout; }
            set { layout = value;
                notifyPropertyChanged("Layout");}
        }

        public bool Overlay
        {
            get { return overlay; }
            set { overlay = value;
                notifyPropertyChanged("Overlay"); }
        }

        public ShapeProperties ShapeProperties
        {
            get { return spPr; }
            set { spPr = value;
                notifyPropertyChanged("ShapeProperties");}
        }

        public TextBody TextProperties
        {
            get { return txPr; }
            set
            {
                txPr = value;
                notifyPropertyChanged("TextProperties");
            }
        }

        public ExtensionElement Extension
        {
            get { return _extLst; }
            set { _extLst = value;
                notifyPropertyChanged("Extension"); }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("title"))
            {
                c.PassAttributes();
                if (c.Reader.LocalName == "numFmt")
                    c.Reader.ReadOuterXml(); //\\hack;
                tx = c.ReadOptionalElement<Text>("tx");
                layout = c.ReadOptionalElement<Layout>("layout");
                overlay = c.ReadOptionalElementBooleanAttribute("overlay", false);
                spPr = c.ReadOptionalElement<ShapeProperties>("spPr");
                txPr = c.ReadOptionalElementAs<TextBody>("txPr");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst"); //\\name?
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("title"))
            {
//\\                if (c.Reader.LocalName == "numFmt")
                //\\                c.Reader.ReadOuterXml();//\\hack;
                c.WriteOptionalElement(tx);
                c.WriteOptionalElement(layout);
                c.WriteOptionalElementBooleanAttribute("overlay", overlay);
                c.WriteOptionalElement(spPr);
                c.WriteOptionalElementAs(txPr, "txPr");
                c.WarnIf("extLst");
            }
        }

        #endregion

        /*
        public Brush Background
        {
            get
            {
                if (spPr == null)
                    return null;
                return spPr.Fill;
            }
        }
	*/

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        void notifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}